package com.zhao.dota.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhao.dota.bo.PlayerMax;
import com.zhao.dota.model.Performance;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
public interface PerformanceMapper extends BaseMapper<Performance> {

    @Select("<script>" +
            "select sum(kills) as kills, sum(deaths) as deaths, sum(assists) as assists, " +
            "   sum(total_earned_gold) as totalEarnedGold, sum(last_hit_count) as lastHitCount, " +
            "   sum(deny_count) as denyCount, sum(hero_damage) as heroDamage, sum(total_earned_xp) as totalEarnedXp, " +
            "   sum(first_blood_claimed) as firstBloodClaimed, sum(first_blood_given) as firstBloodGiven, " +
            "   sum(healing) as healing, sum(tower_damage) as towerDamage, sum(gpm) as gpm, sum(xpm) as xpm " +
            "  from performance where steam_id = #{steamId} " +
            " order by end_time desc" +
            "<if test='limit > 0'>" +
            " limit #{limit}" +
            "</if> " +
            "</script>")
    Performance countByPlayer(Long steamId, int limit);

    @Select("SELECT " +
            "  c.*,d.${field} as `max`  " +
            "  FROM match_player c " +
            "  LEFT JOIN ( " +
            "  SELECT " +
            "    match_player_id, " +
            "    ${field}  " +
            "    FROM performance a " +
            "   WHERE ${field} = ( SELECT  max( ${field} ) FROM performance WHERE a.steam_id = steam_id ) " +
            "     AND steam_id = #{steamId}  " +
            "   limit 1 " +
            "  ) d " +
            "    ON c.id = d.match_player_id  " +
            " where d.match_player_id is not null")
    PlayerMax max(String field, Long steamId);

}
